package com.example.api.task;


import com.example.api.constant.BookConst;
import com.example.api.pojo.Book;
import com.example.api.query.BookQuery;
import com.example.api.service.BookService;
import com.example.api.utils.PageResult;
import com.example.api.vo.BookListVO;
import com.example.api.vo.BookVO;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class BookTask {

    @Resource
    BookService bookService;

   @Scheduled(cron=" 0 */1 * * * ?")
    public void timeOutBook(){

       BookQuery bookQuery = new BookQuery();
       bookQuery.setStatus(BookConst.PENDING_PAYMENT);



       Calendar calendar = Calendar.getInstance();
       calendar.add(Calendar.MINUTE,-1);

       Date date = calendar.getTime();
       bookQuery.setEndTime(date);


       PageResult<BookListVO> pageResult = bookService.find(bookQuery);

       List<BookListVO> bookListVOList = pageResult.getList();

       for(BookListVO bookListVO : bookListVOList){

           Book book = new Book();
           book.setNum(bookListVO.getNum());

           book.setCancelReason("订单超时");
           book.setCancelTime(new Date());
           book.setStatus(BookConst.CANCELLED);
           bookService.cancel(book.getNum(), book.getCancelReason());
       }



    }
}
